package com.terrorism.first.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.terrorism.first.R;
import com.terrorism.first.server.UserConnect;
import com.terrorism.sdk.utils.TextUtils;
import com.terrorism.sdk.view.CustomToast;

public  class LoginFragment extends Fragment {


	Activity mActivity;
	TextView tvAccount, tvPassword, tvEmail, tvPhone, tvName, tvGender, tvAge;
	EditText edAccount, edPassword, edEmail, edPhone, edName, edGender, edAge;
	Button btnSignUp;


	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mActivity = activity;

	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState){
		View v = inflater.inflate(R.layout.fragment_signup, container, false);
		tvAccount =(TextView) v.findViewById(R.id.tvSignUpAcc);
		tvPassword =(TextView) v.findViewById(R.id.tvSignUpPassword);
		tvEmail =(TextView) v.findViewById(R.id.tvSignUpEmail);
		tvPhone =(TextView) v.findViewById(R.id.tvSignUpPhone);
		tvName =(TextView) v.findViewById(R.id.tvSignUpName);
		tvGender =(TextView) v.findViewById(R.id.tvSignUpGender);
		tvAge =(TextView) v.findViewById(R.id.tvSignUpAge);
		edAccount=(EditText) v.findViewById(R.id.userAccount);
		edPassword=(EditText) v.findViewById(R.id.userPassword);
		edEmail=(EditText) v.findViewById(R.id.userEmail);
		edPhone=(EditText) v.findViewById(R.id.userPhone);
		edName=(EditText) v.findViewById(R.id.userName);
		edGender=(EditText) v.findViewById(R.id.userGender);
		edAge=(EditText) v.findViewById(R.id.userAge);



		btnSignUp =(Button) v.findViewById(R.id.btnSignUp);
		btnSignUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				signUp();
			}
		});



		return v;


	}


	public void signUp(){
		if (TextUtils.isAccountPasswordOk(edAccount.getText().toString()) &&
				TextUtils.isAccountPasswordOk(edPassword.getText().toString()) && 
				TextUtils.isEmailOk(edEmail.getText().toString()) &&
				edPhone.getText().toString()!=null &&
				edName.getText().toString()!=null) {

			Log.e("isValid", 
					" "+ true);
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					final JSONObject jo = UserConnect.signUp(edAccount.getText().toString(), edPassword.getText().toString(), edEmail.getText().toString(), edPhone.getText().toString(), edName.getText().toString(), edGender.getText().toString(), edAge.getText().toString());

					mActivity.runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							//int result;
							try {
								if (jo.getString("result").equals("1")) {
									CustomToast.vToastShort(mActivity, jo.getString("message"));
									// sign up status
								} else if(jo.getString("result").equals("2")){
									CustomToast.vToastShort(mActivity, jo.getString("message"));
								}else if(jo.getString("result").equals("3")){
									CustomToast.vToastShort(mActivity, jo.getString("message"));
								}else if(jo.getString("result").equals("4")){
									CustomToast.vToastShort(mActivity, "message");
								}


							} catch (JSONException e) {
								// TODO: handle exception
								e.printStackTrace();
							}
						}
					});
				}
			}).start();

		}
		else {
			Toast.makeText(mActivity, "sai thong tin",1000).show();
		}
	}

}
